var Line = require('./Line.js');
var Project = require('./Project.js');
var extend = require('extend');

function City(name) {
    this.name = name || "";
    this.lines = [];
}

City.prototype.test = function () {
    console.log('test');
};

//把前端的prjlist渲染成city的列表
City.parseProjectsToCityList = function (prjList, typeCode) {
    var cityList = [];
    var tempCity = new City();
    var tempLine = new Line();
    var filterPrjTypes = [];
    var filterPrjCodes = [];//储存临时的筛选工程类型列表
    typeCode = typeCode || "";
    var lineId = 0;
    prjList.forEach(function (prj, index, arr) {
        //刷选工程类型
        if (filterPrjTypes.indexOf(prj.projectType) < 0) {
            filterPrjTypes.push(prj.projectType);
            filterPrjCodes.push(prj.projectTypeCode);
        }
        if (prj.projectTypeCode.indexOf(typeCode) < 0) return;
        if (prj.location !== tempCity.name) {
            tempCity = new City(prj.location);
            cityList.push(tempCity); //加入城市列表
        }
        if (prj.lineName !== tempLine.name) {
            lineId = lineId + 1;
            tempLine = new Line(prj.lineName, lineId);
            tempCity.lines.push(tempLine);
        }
        var prjItem = extend(new Project(prj), prj);
        tempLine.projects.push(prjItem);
    });
    return {
        cityList: cityList,
        filterPrjTypes: filterPrjTypes.map(function (item, index) {
            return {
                projectType: item,
                projectCode: filterPrjCodes[index]
            }
        })
    };
};


module.exports = City;